let Utils = {
  // 设置缓存
// let users = this.Utils.storage.get('users');
// if (users) {}
  storage: {
      set (key, value, expiration = 0) {
          let obj = {}
          obj.data = value
          obj.setTime = new Date().getTime()
          if (expiration) {
              obj.expiration = 60 * 60 * (24 * expiration)
          }
          localStorage.setItem(key, JSON.stringify(obj))
      },
      get (key) {
          let [item = JSON.parse(localStorage.getItem(key)), exp = null, nowTime = new Date().getTime()] = []
          if (item) {
              if (item.data && item.data.expires_in) {
                  exp = item.data.expires_in
              } else if (item.expiration) {
                  exp = item.expiration
              }
              if (exp && nowTime - item.setTime > exp * 1000) {
                  localStorage.removeItem(key)
                  return false
              }
          }
          return item
      },
      remove(key) {
          localStorage.removeItem(key)
      }
  },


  
  // 去除字符串中所有空格
  // !this.Utils.removeAllSpace(this.fundPassword.new)
  removeAllSpace (str) {
      return str.replace(/\s+/g, '')
  },

  // 限制 只输入数字 包括不能输入字符和小数点
//  @input="bank.card_number = Utils.number(bank.card_number)" v-model="bank.card_number"
  number (str, float = false) {
    str = String(str);
    // float = true 可以输入小数点
    if (float) {
      return str.replace(/[^\d.]/g, '').replace(/^0{1,}/g,'')
    }
    else {
      return str.replace(/[^\d]/g, '').replace(/^0{1,}/g,'')
    }
  },

  // 禁止输入汉字 和 空格
//  @input="users.username = Utils.noChinese(users.username)"
  noChinese (str) {
    return str.replace(/[\u4E00-\u9FA5]| | [\uFE30-\uFFA0]/g,'').replace(/(^\s+)|(\s+$)/g, '');
  },
 // 去除字符串前后的空格
 // this.Utils.trim(item)
  trim (str) {
      return str.replace(/(^\s+)|(\s+$)/g, '')
  },
  // 去除字符串换行
  removeLineFeed (str) {
      return str.replace(/[\n]/g, '')
  },
  // 去除字符串回车
  removeEnter (str) {
      return str.replace(/[\r]/g, '')
  },
// if (this.Utils.checkIsChinese(number)) {}
  // 是否包含汉字
  checkIsChinese (val) {
    let reg = new RegExp('[\\u4E00-\\u9FFF]+', 'g')
    if (reg.test(val)) {
      return true
    }
  },
  /*
  * 显示小数点后面几位
  * str 字符串
  * num 显示小数点后几位小数
  */
// Utils.toFixed(String(user.balance))
  toFixed (str, num = 2) {
      if (String(str).indexOf('.') > -1) {
        let zeo = ''
        for (let i = 0; i < (num - String(str).substr(String(str).lastIndexOf('.') + 1, str.length).length); i++) {
          zeo += '0'
        }
          return String(str).substr(0, String(str).lastIndexOf('.') + num + 1) + zeo
      } else {
          let zeo = ''
          for (let i = 0; i < num; i++) {
              zeo += '0'
          }
          return String(str) + '.' + zeo
      }
  },
  // 将剩余秒数转换成倒计时
// this.Utils.formatSeconds(this.time)
  formatSeconds (value) {
      let secondTime = parseInt(value);
      let minuteTime = 0;
      let hourTime = 0;
      if (secondTime > 60) {
          minuteTime = parseInt(secondTime / 60);
          secondTime = parseInt(secondTime % 60);
          if (minuteTime > 60) {
              hourTime = parseInt(minuteTime / 60);
              minuteTime = parseInt(minuteTime % 60);
          }
      }
      if (secondTime < 10) {
          secondTime = '0' + parseInt(secondTime);
      }
      if (minuteTime < 10) {
          minuteTime = '0' + parseInt(minuteTime);
      }
      if (hourTime < 10) {
          hourTime = '0' + parseInt(hourTime);
      }
      return hourTime + ':' + minuteTime + ':' + secondTime;
  },
  // obj 传入时间  返回 yyyy-mm-dd
// Utils.formatTime(item.time_bought)
formatTime(obj, format = 'YYYY-MM-DD HH:MM:SS') {
  if (!obj) {
    return;
  }
  if (typeof obj === 'string') {
    obj = Number(obj);
  }
  if (String(new Date(obj).getTime()).length < 13) {
    obj = new Date(obj).getTime() * 1000;
  }
  let date = new Date(obj);
  let date2 = date.toLocaleDateString().split('/');
  let hours = date.getHours();
  let minutes = date.getMinutes();
  let seconds = date.getSeconds();
  if (date2[1] < 10) {
    date2[1] = '0' + date2[1]
  }
  if (date2[2] < 10) {
    date2[2] = '0' + date2[2]
  }
  if (hours < 10) {
    hours = '0' + hours
  }
  if (minutes < 10) {
    minutes = '0' + minutes
  }
  if (seconds < 10) {
    seconds = '0' + seconds
  }
  let dateDay = date2.join('-');
  if (format === 'YYYY-MM-DD') {
    return dateDay;
  } else if (format === 'HH:MM:SS') {
    return hours + ":" + minutes + ':' + seconds
  } else if (format === 'YYYY-MM-DD HH:MM:SS') {
    return dateDay + ' ' + hours + ":" + minutes + ':' + seconds
  }
},
// 过滤json
// this.Utils.filterData(this.searchs)
filterData(data) {
  for (const k of Object.keys(data)) {
    if (data[k] == '' || data[k] == ' ') {
      delete data[k];
    }
  }
  return data;
},
// 处理文本中img请求头
// this.Utils.setImg(k['content'], 'add');
setImg(str, type) {
  let imgReg = /<img.*?(?:>|\/>)/gi;
  // eslint-disable-next-line no-useless-escape
  let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;
  let arr = str.match(imgReg);
  let st = str;
  if(arr){
    for (let i = 0; i < arr.length; i++) {
      let src = arr[i].match(srcReg);
      if(type === 'remove'){
        let replace_src = src[1].replace(process.env.imgURL,'');
        st = st.replace(src[1], replace_src);
      } else if(type === 'add'){
        st = st.replace(src[1],  process.env.imgURL + src[1]);
      }
    }
  }
  return st;
},

fmtDate(obj){
  if(obj) {
   let date = new Date(obj)
   let Y = date.getFullYear();
   let M = date.getMonth() + 1;
   let D = date.getDate();

   if(M < 10) {
       M = '0' + M;
   }
   if(D < 10) {
       D = '0' + D;
   }

   return Y + '-' + M + '-' + D ;
} else {
   return '';
}
},

isMobile() {
  let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
  return flag;
}




};
export default Utils